import HttpProvider from 'HttpProvider'
import Util from 'util'
const app = getApp()

class RPCService extends HttpProvider {
    constructor() {
      super();

      this.rpcPath = {
          // 公共
          wechatSignIn: '/app/user/loginByWX', // 微信用户登陆
          sendCode: '/app/visitor/sendMsg', // 发送手机验证码
          uploadImage: '/app/visitor/uploadNeedsFile', // 上传图片
          licenceType: '/app/visitor/getLicenseType', // 获取执业类别
          label: '/app/visitor/getLabel', // 获取标签，
          province: '/app/visitor/getProvince',
          city: '/app/visitor/getCity',
          area: '/app/visitor/getArea',
          // B端用户接口
          judgeRole: '/app/visitor/judgeIsHave', // 判断用户是否初次
          register: '/app/visitor/needsRegister', // B端用户注册
          launchDemand: '/app/user/postNeed', // B端用户发布需求
          bDemandList: '/app/user/getNeedList', // B端用户需求列表
          bDemandDetail: '/app/user/getNeedDetail', // B端用户需求详情
          saveComments: '/app/user/saveComment' // 保存评论
      }
    }

    // 微信注册
    wechatSignUp(params) {
        return this.doPostRequest(
            this.rpcPath.wechatSignIn,
            params
        );
    }
    // 发送手机验证码
    sendCode(params) {
      return this.doPostRequest(
        this.rpcPath.sendCode,
        params
      );
    }
    // 发送图片
    sendImage(params) {
      return this.uploadFile(
        this.rpcPath.uploadImage,
        params
      );
    }
    // 获取执业类别
    getLicenceType(params) {
      return this.doGetRequest(
        this.rpcPath.licenceType,
        params
      );
    }
    // 获取标签
    getLabel(params) {
      return this.doGetRequest(
        this.rpcPath.label,
        params
      );
    }
    // 获取标签
    getProvince(params) {
      return this.doGetRequest(
        this.rpcPath.province,
        params
      );
    }
    // 获取城市
    getCity(params) {
      return this.doGetRequest(
        this.rpcPath.city,
        params
      );
    }
    // 获取区县
    getArea(params) {
      return this.doGetRequest(
        this.rpcPath.area,
        params
      );
    }


    // 判断用户是否初次进入
    judgeRole(params) {
      return this.doPostRequest(
        this.rpcPath.judgeRole,
        params
      );
    }
    // B端用户注册
    register(params) {
      return this.doPostRequest(
        this.rpcPath.register,
        params
      );
    }
    // B端用户发布需求
    launchDemand(params) {
      return this.doPostRequest(
        this.rpcPath.launchDemand,
        params
      );
    }
    // B端用户获取需求列表
    getBDemandList(params) {
      return this.doGetRequest(
        this.rpcPath.bDemandList,
        params
      );
    }
    // 获取需求详情
    getBDemandDetail(params) {
      return this.doGetRequest(
        this.rpcPath.bDemandDetail,
        params
      );
    }
    // 保存评论
    saveComments(params) {
      return this.doPostRequest(
        this.rpcPath.saveComments,
        params
      );
    }

    /**
     * 上传文件
     * @param {name,filePath,params}
     */
    uploadFile(url, options) {
        wx.showLoading({
            title: '上传中...',
            mask: true
        })

        let _self = this,
            headerParams = this.getSessionHeader({
                'content-type': 'multipart/form-data'
            });

        return new Promise((resolve, reject) => {
            wx.uploadFile({
                url: url,
                filePath: options.filePath,
                name: options.name || 'recordFile',
                header: headerParams,
                formData: options.params,
                success: function (res) {
                    wx.hideLoading()
                    if (res.statusCode == 200) {
                        resolve(res.data);
                    } else {
                        reject(res);
                    }
                },
                fail: function (err) {
                    wx.hideLoading()
                    reject(err)
                }
            })
        })
    }

}

export default RPCService;